<?php

/**
 * Created by PhpStorm.
 * User: william
 * Date: 16-8-31
 * Time: 下午2:44
 */
namespace V1;
class PayTypeList extends \ResourceClass
{
    public $isActive = true;
    public $remark = '获取企业自定义支付类型';

    public function get(array $req, array $post, &$result)
    {
        if (!isset($req['uid'])) {
            return 'require user id ';
        }
        if (!isset($req['userType'])) {
            return 'require user type';
        }
        if (!isset($req['k']) || !isset($req['t'])) {
            return 'data error';
        }

        /* 授权验证*/
        if (\Biz\UserHandler::checkAuth($req['userType'], $req['uid'],$req['enterpriseId'], $req['t'], $req['k']) == false) {
            return 'data illegal';
        }

        if (!isset($req['enterpriseId'])) {
            return 'require enterprise id';
        }
        if (!isset($req['channelId'])) {
            return 'require channel id';
        }

        try {
            $result['payTypeList'] = array(
                array(
                    'type' => '1',
                    'name' => '线上支付',
                    'is_checked' => 1,
                    'items' => array()
                ),
                array(
                    'type' => '0',
                    'name' => '线下支付',
                    'is_checked' => 0,
                    'items' => array()
                )
            );

            $pay_handler = new \Biz\PayHandler();
            $online_pay_list  = $pay_handler->getOnlineByEnterpriseId($req['enterpriseId']);
            $online_pay_list = isset($online_pay_list) ? $online_pay_list : array();
            $offline_pay_list = $pay_handler->getOfflineByEnterpriseId($req['enterpriseId']);
            $offline_pay_list = isset($offline_pay_list) ? $offline_pay_list : array();

            foreach ($result['payTypeList'] as $key => $value) {
                if ($value['type'] == '1') {
                    $result['payTypeList'][$key]['items'] = $this->pieceOnline($online_pay_list);
                } else if ($value['type'] == '0') {
                    $result['payTypeList'][$key]['items'] = $this->pieceOffline($offline_pay_list);
                }
            }
        } catch (\Exception $e) {
            return $e->getMessage();
        }
    }

    /*
     * 拼凑在线支付
     * @param  Cls\Pay\Online  $online_cls
     */
    private function pieceOnline($online_list)
    {
        $online = array();
        $online_info = array();
        foreach ($online_list as $online_cls) {
            $online['id']         = $online_cls->getId();
            $online['code']       = $online_cls->getCode();
            $online['name']       = $online_cls->getPayTypeName();
            $online['icon']       = $online_cls->getIcon();
            $online['is_checked'] = $online_cls->getDefault();
            $online_info[]        = $online;
        }
        return $online_info;
    }

    /*
     * 拼凑线下支付
     * @param  Cls\Pay\Offline  $offline_cls
     */
    private function pieceOffline($offline_list)
    {
        $offline = array();
        $offline_info = array();
        foreach ($offline_list as $offline_cls) {
            $offline['id']          = $offline_cls->getId();
            $offline['bank_name']   = $offline_cls->getBankName();
            $offline['bank_number'] = $offline_cls->getBankNumber();
            $offline['account']     = $offline_cls->getAccount();
            $offline['is_checked']  = $offline_cls->getDefault();
            $offline_info[]         = $offline;
        }
        return $offline_info;
    }
}